package cn.example.fragmenttest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(this);
//        replaceFragment(new RightFragment());
    }

//    private void replaceFragment(Fragment fragment) {
//        FragmentManager fragmentManager = getSupportFragmentManager();
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        transaction.replace(R.id.right_layout, fragment);
//        transaction.addToBackStack(null);// 添加事务到返回栈
//        transaction.commit();
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
//                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }
    }

    public void funInActivity(){
        Log.i(TAG, "funInActivity: Hello");
    }

    // 活动与碎片通信
//    private void communicateWithFragment(){
//        RightFragment fragment = (RightFragment) getSupportFragmentManager().findFragmentById(R.id.right_layout);
//        fragment.funInRightFragment();
//    }
}